package cn.xm.system.service.impl;

import cn.xm.system.domain.Role;
import cn.xm.system.domain.RoleMenu;
import cn.xm.system.mapper.RoleMenuMapper;
import cn.xm.system.mapper.RolePermissionMapper;
import cn.xm.system.service.RoleService;
import cn.xm.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xm
 * @since 2022-10-24
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void add(Role role) {
        super.add(role);
        Long rid = role.getId();
        List<Long> menus = role.getMenus();
        roleMenuMapper.add(rid,menus);
        List<Long> permissions = role.getPermissions();
        rolePermissionMapper.add(rid,permissions);

    }

    @Override
    public void delete(Long id) {
        super.delete(id);
        roleMenuMapper.deleteByRoleId(id);
        rolePermissionMapper.deleByRoleId(id);
    }

    @Override
    public void update(Role role) {
        super.update(role);
        Long rid = role.getId();
        List<Long> menus = role.getMenus();
        if (menus != null) {
            roleMenuMapper.deleteByRoleId(rid);
            roleMenuMapper.add(rid,menus);
        }
        List<Long> permissions = role.getPermissions();
        if (permissions != null) {
            rolePermissionMapper.deleByRoleId(rid);
            rolePermissionMapper.add(rid,permissions);
        }

    }
}
